package com.vdg.lockphotos.data;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import android.content.ContentUris;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.graphics.YuvImage;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.util.Log;

import com.vdg.lockphotos.util.Utilities;

public class Photo implements Parcelable {
	private String mID;
	private String mName;
	private String mDes;
	private String mImageUrl;
	private boolean mIsSelected;
	private String mHiddenUrl;
	private String mAlbumName;

	public Photo(String name, String url) {
		this.mName = name;
		this.mImageUrl = url;
	}

	public String getID() {
		return mID;
	}

	public void setID(String mID) {
		this.mID = mID;
	}

	public String getName() {
		return mName;
	}

	public void setName(String mTitle) {
		this.mName = mTitle;
	}

	public String getDes() {
		return mDes;
	}

	public void setDes(String mDes) {
		this.mDes = mDes;
	}

	public String getImageUrl() {
		return mImageUrl;
	}

	public void setImageUrl(String url) {
		this.mImageUrl = url;
	}

	public boolean isIsSelected() {
		return mIsSelected;
	}

	public void setIsSelected(boolean mIsSelected) {
		this.mIsSelected = mIsSelected;
	}

	public boolean hide(Context context, boolean isUpdateInMedia)
			throws NameNotFoundException {
		try {
			mHiddenUrl = Utilities.copy(new File(mImageUrl), mName, context,
					isUpdateInMedia);
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public boolean Show(Context context, boolean isUpdateInMedia) {
		try {
			Utilities.copy(new File(mHiddenUrl), new File(mImageUrl), context,
					isUpdateInMedia);
			return true;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	public String getHiddenUrl() {
		return mHiddenUrl;
	}

	public void setHiddenUrl(String mHiddenUrl) {
		this.mHiddenUrl = mHiddenUrl;
	}

	public String toString() {
		return mImageUrl + ";" + mName + ";" + mHiddenUrl;
	}

	public Photo(String photostr) {
		if (photostr != null && !photostr.equals("") && photostr.contains(";")) {
			String[] elements = photostr.split(";");
			if (elements.length == 3) {
				mImageUrl = elements[0];
				mName = elements[1];
				mHiddenUrl = elements[2];
			}
		}
	}

	public static String arrayListToString(ArrayList<Photo> listPhoto) {
		String result = "";
		for (int i = 0; i < listPhoto.size(); i++) {
			if (i == 0) {
				result = listPhoto.get(i).toString();
			} else {
				result = result + ":" + listPhoto.get(i).toString();
			}
		}
		return result;
	}

	public static void addHiddenInSharedPreferences(Context context,
			Photo phtoHiddent) {
		ArrayList<Photo> listPhotoStore = new ArrayList<Photo>();
		String sto = Utilities.getStringFromSharedPreferences("Photo", context);
		if (sto != null && !sto.equals("")) {
			if (sto.contains(":")) {
				String[] strArrPhoto = sto.split(":");
				for (String strphoto : strArrPhoto) {
					Photo photo = new Photo(strphoto);
					listPhotoStore.add(photo);
				}
			} else {
				if (sto.contains(";")) {
					listPhotoStore.add(new Photo(sto));
				}
			}
		}
		if (!listPhotoStore.contains(phtoHiddent)) {
			listPhotoStore.add(phtoHiddent);
		}
		Utilities.saveStringToSharedPreferences(
				arrayListToString(listPhotoStore), context, "Photo");
	}

	public static String removeHiddenInSharedPreferences(Context context,
			Photo phtoHiddent) {
		ArrayList<Photo> listPhotoStore = new ArrayList<Photo>();
		String sto = Utilities.getStringFromSharedPreferences("Photo", context);
		if (sto != null && !sto.equals("")) {
			if (sto.contains(":")) {
				String[] strArrPhoto = sto.split(":");
				for (String strphoto : strArrPhoto) {
					Photo photo = new Photo(strphoto);
					listPhotoStore.add(photo);
				}
			} else {
				if (sto.contains(";")) {
					listPhotoStore.add(new Photo(sto));
				}
			}
		}
		Log.v("debug",
				"string store = " + sto + "-------"
						+ phtoHiddent.getHiddenUrl());
		for (Photo p : listPhotoStore) {
			if (p.getHiddenUrl().equals(phtoHiddent.getHiddenUrl())) {
				Log.v("debug", "string store1 = " + p.getImageUrl());
				String orgUrl = p.getImageUrl();
				listPhotoStore.remove(p);
				Utilities.saveStringToSharedPreferences(
						arrayListToString(listPhotoStore), context, "Photo");
				return orgUrl;
			}
		}
		return null;
	}

	public String getAlbumName() {
		return mAlbumName;
	}

	public void setAlbumName(String mAlbumName) {
		this.mAlbumName = mAlbumName;
	}

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel parcel, int flags) {
		parcel.writeString(getID());
		parcel.writeString(getName());
		parcel.writeString(getImageUrl());
		parcel.writeString(getAlbumName());
		parcel.writeString(getDes());
		parcel.writeString(getHiddenUrl());
	}

	public static final Parcelable.Creator<Photo> CREATOR = new Parcelable.Creator<Photo>() {
		public Photo createFromParcel(Parcel in) {
			return new Photo(in);
		}

		public Photo[] newArray(int size) {
			return new Photo[size];
		}
	};

	private Photo(Parcel in) {
		super();
		this.mID = in.readString();
		this.mName = in.readString();
		this.mImageUrl = in.readString();
		this.mAlbumName = in.readString();
		this.mDes = in.readString();
		this.mHiddenUrl = in.readString();
	}
}
